//
//  LGTRWZUrlStepCell.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/24.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTRWZPostDetailUrlStepCell: UITableViewCell {
    var titleLabel:UILabel?
    var stepLabel:UILabel?
    var stepCellClickBlock:ClickStepPathBlock?
    var returnStrBlock:ClickCellStrBlock?  //返回给j交互视图
    var bottomClickBlock:ClickIndexBlock?
    
    private var _model:Dictionary<String, Any>?
    var path:IndexPath?
    var model:Dictionary<String, Any>{
        get{
            return _model ?? Dictionary.init()
        }set{
            _model = newValue
            self.setData()
        }
    }
    
    func setData(){
        self.titleLabel?.text = self.model["stepText"] as? String ?? ""
        self.stepLabel?.text = "\((self.path?.row ?? 1))"
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        
        self.titleLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: "扫描下方二维码")
        self.titleLabel?.numberOfLines = 0
        self.addSubview(self.titleLabel!)
        self.titleLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(41))
            make.right.equalTo(-SizeFitFloat(48))
            make.top.equalTo(SizeFit(22))
        }
        
        self.stepLabel =  LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: UIColor.white, fontSize: SizeFitFloat(16), text: "1", corner: SizeFitFloat(10))
        self.addSubview(self.stepLabel!)
        self.stepLabel?.backgroundColor = YellowColor
        self.stepLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.height.width.height.equalTo(SizeFit(20))
            make.centerY.equalTo(self.titleLabel!)
        }
        
        let openurlBtn:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "打开链接", font: Font_Normal_15, color: UIColor.white, selectedColor: UIColor.white, dW: 0, dH: 0, corner: SizeFitFloat(5), backColor: YellowColor)
        openurlBtn.tag = StepCellClickType.openurl.rawValue
        self.addSubview(openurlBtn)
        let backTop:Double = SizeFit(18)
        openurlBtn.snp.makeConstraints { (make) in
            make.width.equalTo(SizeFit(108))
            make.height.equalTo(SizeFit(31))
            make.left.equalTo(SizeFit(44))
            //make.top.equalTo(SizeFit(55))
            make.top.equalTo(self.self.titleLabel!.safeAreaLayoutGuide.snp.bottom).offset(backTop)
            if #available(iOS 11.0, *){
                make.top.equalTo(self.self.titleLabel!.safeAreaLayoutGuide.snp.bottom).offset(backTop)
            }else{
                make.top.equalTo(self.self.titleLabel!.snp_bottomMargin).offset(backTop)
            }
            //make.top.equalTo(self.titleLabel!.snp_bottomMargin).offset(SizeFit(18))
        }
        
        
        let copyurlBtn:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "复制链接", font: Font_Normal_15, color: YellowColor, selectedColor: YellowColor, dW: 0, dH: 0, corner: SizeFitFloat(5), backColor: UIColor.white, borderWidth:1, borderColor: YellowColor)
        copyurlBtn.tag = StepCellClickType.copyurl.rawValue
        self.addSubview(copyurlBtn)
        copyurlBtn.snp.makeConstraints { (make) in
            make.width.equalTo(SizeFit(108))
            make.height.equalTo(SizeFit(31))
            make.left.equalTo(openurlBtn.snp_rightMargin).offset(SizeFit(19))
            make.centerY.equalTo(openurlBtn)
            //make.top.equalTo(SizeFit(55))
            //make.top.equalTo(self.titleLabel!.snp_bottomMargin).offset(SizeFit(18))
        }
    
        
        let imageLine:UIImageView = LGTViewCreateUtil.share.createLineView()
        self.addSubview(imageLine)
        imageLine.backgroundColor = ColorUtil(color: 0xEFEFEF)
        imageLine.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.right.equalTo(-SizeFit(11))
            make.height.equalTo(1)
            make.top.equalTo(copyurlBtn.snp_bottomMargin).offset(SizeFit(22))
            make.bottom.equalTo(0)
        }
        
        openurlBtn.addTarget(self, action: #selector(gotoAction), for: UIControl.Event.touchUpInside)
        copyurlBtn.addTarget(self, action: #selector(gotoAction), for: UIControl.Event.touchUpInside)
    }
    
    @objc func gotoAction(sender:UIButton){
        self.gotoActionBlock(index: sender.tag)
    }
    
    func gotoActionBlock(index:Int){
        
        if self.stepCellClickBlock != nil{
            self.stepCellClickBlock!(self.model, self.path!, index, nil)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
